<?php

namespace api\controller;

// +----------------------------------------------------------------------
// | 租户认证数据控制层
// +----------------------------------------------------------------------

use common\controller\VerifyController;
use common\exception\LogicException;
use common\service\TenantAuthenticationService;
use common\validate\TenantAuthentication;
use support\ErrorCode;
use think\Request;

class TenantAuthenticationController extends VerifyController
{
    /**
     * @var \common\service\TenantAuthenticationService
     */
    protected $tenantAuthenticationService;


    public function __construct()
    {
        parent::__construct();
        $this->tenantAuthenticationService = new TenantAuthenticationService();
    }

    /**
     * 提交认证
     * @param Request $request
     * @return \think\Response
     * @throws \Throwable
     */
    public function create(Request $request)
    {
        try {
            $param = $request->param();
            $res = $this->tenantAuthenticationService->create($param['data']);
            return json(success_response("", $res));
        } catch (LogicException $e) {
            throw_strack_exception($e->getMessage(), $e->getCode());
        }
    }

    /**
     * 审核
     * @param Request $request
     * @return \think\Response
     * @throws \Throwable
     */
    public function review(Request $request)
    {

        $param = $request->param();
        if ($param['data']['status'] == "active") {
            $validator = new TenantAuthentication();
            $validator->scene("LevelData");
            foreach ($param['data']['level_data'] as $levelSetup) {
                if (!$validator->check($levelSetup)) {
                    throw_strack_exception("[level_data] " . $validator->getError(), ErrorCode::LEVEL_REQUEST_DATA_PARAM);
                }
            }
        }
        try {
            $res = $this->tenantAuthenticationService->review($param['data']);
            return json(success_response("", $res));
        } catch (LogicException $e) {
            throw_strack_exception($e->getMessage(), $e->getCode());
        }
    }

    /**
     * 查询
     * @param Request $request
     * @return \think\Response
     */
    public function find(Request $request)
    {
        $param = $request->param();
        $res = $this->tenantAuthenticationService->find($param['param']);
        return json(success_response("", $res));
    }

    /**
     * 查询
     * @param Request $request
     * @return \think\Response
     */
    public function getSupplierDetails(Request $request)
    {
        $param = $request->param();
        $res = $this->tenantAuthenticationService->getSupplierDetails($param['param']['filter']['supplier_id']);
        return json(success_response("", $res));
    }

    /**
     * 修改租户等级
     * @param Request $request
     * @return \think\Response
     * @throws \Throwable
     * @throws \think\Exception
     */
    public function changeTenantStepCategoryLevel(Request $request)
    {
        try {
            $res = $this->tenantAuthenticationService->changeTenantStepCategoryLevel($request->param()['data']);
            return json(success_response("OK", $res));
        } catch (LogicException $e) {
            throw_strack_exception($e->getMessage(), $e->getCode());
        }
    }

    /**
     * 查询加密后的认证数据
     * @param Request $request
     * @return \think\Response
     */
    public function findTenantAuthentication(Request $request): \think\Response
    {
        try {
            $param = $request->param();
            $res = $this->tenantAuthenticationService->findTenantAuthentication($param['param']);
            return json(success_response("", $res));
        } catch (LogicException $e) {
            throw_strack_exception($e->getMessage(), $e->getCode());
        }
    }

    /**
     * 提交营业执照
     * @param Request $request
     * @return \think\Response|void
     * @throws \Exception
     */
    public function businessLicenseSubmit(Request $request)
    {
        try {
            $param = $request->param();
            $res = $this->tenantAuthenticationService->businessLicenseSubmit($request->tenantId, $param['data']);
            return json(success_response("", $res));
        } catch (LogicException $e) {
            throw_strack_exception($e->getMessage(), $e->getCode());
        }
    }

    /**
     * 提交法人证件照
     * @param Request $request
     * @return \think\Response|void
     * @throws \Exception
     */
    public function legalPersonSubmit(Request $request)
    {
        try {
            $param = $request->param();
            $res = $this->tenantAuthenticationService->legalPersonSubmit($request->tenantId, $param['data']);
            return json(success_response("", $res));
        } catch (LogicException $e) {
            throw_strack_exception($e->getMessage(), $e->getCode());
        }
    }

    /**
     * 认证信息提交审核
     * @param Request $request
     * @return \think\Response|void
     * @throws \Exception
     */
    public function submitAudit(Request $request)
    {
        try {
            $param = $request->param();
            $res = $this->tenantAuthenticationService->submitAudit($request->tenantId, $param['data']);
            return json(success_response("", $res));
        } catch (LogicException $e) {
            throw_strack_exception($e->getMessage(), $e->getCode());
        }
    }
}
